package com.gy.yemall.yemallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gy.yemall.yemallproduct.entity.AttrAttrgroupRelationEntity;
import com.gy.yemall.yemallproduct.entity.AttrEntity;
import com.gy.yemall.yemallproduct.entity.vo.AttrGroupAndAttrVo;
import com.gy.yemall.yemallproduct.entity.vo.AttrGroupRelationVo;
import com.gy.yemall.yemallproduct.service.AttrAttrgroupRelationService;
import com.gy.yemall.yemallproduct.service.AttrService;
import com.gy.yemall.yemallproduct.service.CategoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gy.yemall.yemallproduct.entity.AttrGroupEntity;
import com.gy.yemall.yemallproduct.service.AttrGroupService;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.R;



/**
 * 属性分组
 *
 * @author ye
 * @email 2900801226@qq.com
 * @date 2021-09-30 16:28:53
 */
@RestController
@RequestMapping("yemallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    // @RequiresPermissions("yemallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Integer catId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils pageUtils = attrGroupService.queryPageBycatId(params, catId);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 根据分组的id进行查询属性表pms_attr
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R queryAtrr(@PathVariable Integer attrgroupId) {
        List<AttrEntity> list = attrAttrgroupRelationService.queryAtrrByGroupId(attrgroupId);

        return R.ok().put("data", list);

    }

    /**
     * 根据分类的id获取下面的分组以及属性
     * @param catelogId
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R queryAttrGroupAndAttr(@PathVariable Long catelogId) {
        List<AttrGroupAndAttrVo> attrVos = attrGroupService.queryAttrGroupAndAttr(catelogId);
        return R.ok().put("data", attrVos);
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                           @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrGroupService.getNoRelation(attrgroupId, params);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("yemallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] catelogPath = categoryService.getCatelogPathById(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("yemallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 新增属性和属性分组的关联
     */
    @PostMapping("/attr/relation")
    public R saveRalation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo) {
        attrAttrgroupRelationService.saveRalation(attrGroupRelationVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("yemallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("yemallproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性和属性分组的关联表
     * 注意如果传的是Json数据PostMapping要和RequestBody一起连用的
     * 有可能是批量删除所以参数是需要一个数组
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo) {
        attrGroupService.deleteRelation(attrGroupRelationVo);
        return R.ok();
    }

}
